/*
 * @Author: 卡卡 1071780426@qq.com
 * @Date: 2023-03-21 12:16:15
 * @LastEditors: CNN 969682281@qq.com
 * @LastEditTime: 2023-06-29 17:55:14
 * @FilePath: \mould\src\utils\date.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import {
    getUserCompanyTreeList, //左侧机构树接口

} from "@/api/request.js";
export default {
    // 左侧树结构
    getTrees() {
        // this.$store.state.userInfo.
        var data = {
            companyId: JSON.parse(sessionStorage.getItem("userInfo")).companyId,
            userId: JSON.parse(sessionStorage.getItem("userInfo")).userId
            // text: "0", // 0其他  1 砼拌合站 2水稳 3沥青

        }
        getUserCompanyTreeList(data)
            .then((res) => {
                if (res.errcode == 0) {
                    var companyTree = res.data.userCompanyTreeList;
                    return companyTree
                    // 列表
                } else {
                    this.$message.error(res.msg);
                    // this.$message.error("获取组织机构失败");
                    return false;
                }
            })
            .catch(() => {
                return false;
            });
    },
    tranListToTreeData(data) {
        //处理树状图数据
        let result = [];
        if (!Array.isArray(data)) {
            return result;
        }
        // 清除已存在的children 重新赋值
        data.forEach((item) => {
            delete item.children;
        });

        let map = {};
        data.forEach((item) => {
            map[item.fCompanyid] = item;
        });

        data.forEach((item) => {
            let parent = map[item.fParentid];
            if (parent) {
                (parent.children || (parent.children = [])).push(item);
            } else {
                result.push(item);
            }
        });
        return result;
    },

}

// 调用
// getAllPark(data => {
// console.log(data); // 成功拿到数据
// });